package kr.co.mustgo.controller.freeboard;

import java.util.Map;

import kr.co.mustgo.domain.freeboard.FreeComment;
import kr.co.mustgo.domain.freeboard.FreeBoard;
import kr.co.mustgo.model.service.freeboard.CommentService;
import kr.co.mustgo.model.service.freeboard.FreeBoardService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/freeboard/")
public class FreeBoardController {

	@Autowired
	private FreeBoardService freeBoardService;
	
	@Autowired
	private CommentService commentService;

	@RequestMapping("write_form.do")
	public String writeForm(){
		return "tiles3/freeBoard/write_form.jsp";
	}
	
	@RequestMapping("writeContent.do")
	public ModelAndView writeContent(FreeBoard freeBoard){
		ModelAndView mv = null;
			freeBoardService.writeContent(freeBoard);
			mv = new ModelAndView("redirect:getContent.do?code="+freeBoard.getCode());

		return mv;
	}
	
	@RequestMapping("getContent.do")
	public String getContent(int code, ModelMap map){
		String url = null;
		try{
			FreeBoard freeBoard = freeBoardService.getContentByCode(code);
			map.addAttribute("freeBoard", freeBoard);
			url = "tiles3/freeBoard/show_content.jsp";
		}catch(Exception e){
			e.printStackTrace();
			map.addAttribute("error_message", "글 조회도중 오류 발생 : "+e.getMessage());
			url = "tiles2/freeBoard/error.jsp";
		}	
		return url;
	}
	
	
	@RequestMapping("list.do")
	public String list(@RequestParam(defaultValue="1")String page, ModelMap model) throws Exception{
		
		String url = null;
		int pageNo = 1;
		try{
			pageNo = Integer.parseInt(page);
		}catch(Exception e){}
		Map map = freeBoardService.getFreeBoardList(pageNo);
		model.addAllAttributes(map);
		url = "tiles3/freeBoard/list.jsp";
		return url;
	}
	
	@RequestMapping("modifyForm.do")
	public String modifyForm(int code, ModelMap map){
		String url = null;
		try{
			FreeBoard freeBoard = freeBoardService.getContentByNoForForm(code);
			url = "tiles3/freeBoard/modify_form.jsp";
			map.addAttribute("freeBoard", freeBoard);
	
		}catch(Exception e){
			e.printStackTrace();
			url = "tiles2/freeBoard/error.jsp";
			map.addAttribute("error_message", "수정할 글 조회도중 오류발생 : "+e.getMessage());
		}
		return url;
	}
	
	@RequestMapping("modifyContent.do")
	public ModelAndView modifyContent(FreeBoard freeBoard, int page){
		ModelAndView mv = null;
		try{
			freeBoardService.modifyContent(freeBoard);
			mv = new ModelAndView("redirect:getContent.do?code="+freeBoard.getCode()+"&page="+page);
		}catch (Exception e){
			e.printStackTrace();
			mv = new ModelAndView("tiles2/freeBoard/error.jsp");
		}
		return mv;
	}
	
	@RequestMapping("deleteContent.do")
	public String deleteContent(int code, int page, ModelMap map){
		String url = null;
		try {
			//2. BL
			freeBoardService.deleteContentByCode(code);
			url = "redirect:list.do?page="+page;
		}catch (Exception e) {
			e.printStackTrace();
			url = "tiles2/freeBoard/error.jsp";
			map.addAttribute("error_message", "삭제도중 오류 발생 : "+e.getMessage());
		}
		return url;
		}
	


	@RequestMapping("comment_writeContent.do")
	public ModelAndView commentWriteContent(FreeComment comment, String page){
		ModelAndView mv = null;
		commentService.writeContent(comment);
		mv = new ModelAndView("redirect:getContent.do?code="+comment.getCode()+"&page="+page);
		return mv;
	}
	
	@RequestMapping("comment_deleteContent.do")
	public String CommentDeleteContent(int reCode, String page, ModelMap map, int code){
		String url = null;
		if(page==null||page.equals("")){
			page="1";
		}
		try {
			commentService.deleteContentByreCode(reCode);
			url = "redirect:getContent.do?page="+page+"&code="+code;
		}catch (Exception e) {
			e.printStackTrace();
			url = "tiles2/freeBoard/error.jsp";
			map.addAttribute("error_message", "삭제도중 오류 발생 : "+e.getMessage());
		}
		return url;
		}
	
	
	
}
